import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;


public class Game {

	/**
	 * @param args
	 */
	public Playground pg;
	Player p1,p2;
	public int playercount = 2;
	int players;
	
	public Game(int width, int height, int players) throws IOException {
		pg = new Playground(width,height,"Map1",this);
		p1 = new Player(width-2,height-2,this,1);
		p2 = new Player(1,1,this,2);
		this.players = players;
		pg.addObject(p1.getX(), p1.getY(), p1);
		pg.addObject(p2.getX(), p2.getY(), p2);
		
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "p1right");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("LEFT"), "p1left");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("UP"), "p1up");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("DOWN"), "p1down");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("SPACE"), "p1bomb");
		
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "p2right");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "p2left");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "p2up");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "p2down");
		pg.pn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("X"), "p2bomb");
		
		pg.pn.getActionMap().put("p1right", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p1.goRight();}		
		} );
		pg.pn.getActionMap().put("p1left", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p1.goLeft();}		
		} );
		pg.pn.getActionMap().put("p1up", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p1.goUp();}		
		} );
		pg.pn.getActionMap().put("p1down", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p1.goDown();}		
		} );
		pg.pn.getActionMap().put("p1bomb", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p1.setBomb();}		
		} );
		
		pg.pn.getActionMap().put("p2right", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p2.goRight();}		
		} );
		pg.pn.getActionMap().put("p2left", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p2.goLeft();}		
		} );
		pg.pn.getActionMap().put("p2up", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p2.goUp();}		
		} );
		pg.pn.getActionMap().put("p2down", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p2.goDown();}		
		} );
		pg.pn.getActionMap().put("p2bomb", new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg0) { p2.setBomb();}		
		} );
	}
	
	public void removeDead() {
		playercount--;
		if (playercount==1) {
			EndGame();
		}
	}

	private void EndGame(){
		System.exit(0);
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new Game(12,10, 2);
	}

}
